package com.china.zhuoyue.estate

import androidx.activity.ComponentActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.china.zhuoyue.api.core.*
import com.china.zhuoyue.api.core.ConvertInterceptor.Companion.JSON_CONTENT_TYPE_HEADER
import kotlinx.coroutines.Job
import retrofit2.http.*

val ESTATE_API_SERVICE: EstateApiService get() = ApiHelper.create(EstateApiService::class.java)
fun <T> ComponentActivity.launchUIEstate(
    block: suspend EstateApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null
): Job = ApiRequest(lifecycleScope, ESTATE_API_SERVICE).launchUI(block, error, complete, apiObserver)

fun <T> Fragment.launchUIEstate(
    block: suspend EstateApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null,
): Job = ApiRequest(lifecycleScope, ESTATE_API_SERVICE).launchUI(block, error, complete, apiObserver)

fun <T> ViewModel.launchUIEstate(
    block: suspend EstateApiService.() -> T,
    apiObserver: Observer<ApiProcess<T>>? = null,
    error: ApiError? = null,
    complete: ApiComplete? = null
): Job = ApiRequest(viewModelScope, ESTATE_API_SERVICE).launchUI(block, error, complete, apiObserver)

interface EstateApiService {

    /**
     * 我的房屋
     * userId	用户id
     */
    @POST("/api/community/myList")
    @Headers(JSON_CONTENT_TYPE_HEADER)
    @FormUrlEncoded
    suspend fun communityMyList(
        @Field("userId") userId: Int,
    ): MutableList<String>?


}